/*--------------------------------------------------------------------------------
	DESCRIPTION: Creation of lifetime experiences datasets

--------------------------------------------------------------------------------*/

cap erase "$data_dir/raw/experience_datasets"
cap mkdir "$data_dir/raw/experience_datasets"

****************************************************************************
**CREATE MERGE PROGRAM WITH BOTH PRE-BIRTH AND LIFETIME EXPERIENCES (dmerge)
****************************************************************************

capture program drop dmerge
program define dmerge

	args data var
	clear
	use "$data_dir/clean/harmonized_dataset.dta"
	
	replace age=18 if yob==2002 & age==17 & ccode=="EGY" & survey_year==2020 //overcomes a duplicate
	
	gen test_dummy = 1
	collapse test_dummy if yob!=-99, by(iso2 ccode survey_year yob age)
	drop test_dummy
	
	scalar age = 99
	cap drop yobs*
	forvalues i = 0/`=scalar(age)' {
		gen yobs_`i' = .
	}
	
	local j = 0
		while `j' <= `=scalar(age)' {
			replace yobs_`j'=yob+`j' if yob!=-99
			local j = `j' + 1
	}
	
	local varlist yobs_0 yobs_1 yobs_2 yobs_3 yobs_4 yobs_5 yobs_6 yobs_7 yobs_8 yobs_9 yobs_10 yobs_11 yobs_12 yobs_13 yobs_14 yobs_15 yobs_16 yobs_17 yobs_18 yobs_19 yobs_20 yobs_21 yobs_22 yobs_23 yobs_24 yobs_25 yobs_26 yobs_27 yobs_28 yobs_29 yobs_30 yobs_31 yobs_32 yobs_33 yobs_34 yobs_35 yobs_36 yobs_37 yobs_38 yobs_39 yobs_40 yobs_41 yobs_42 yobs_43 yobs_44 yobs_45 yobs_46 yobs_47 yobs_48 yobs_49 yobs_50 yobs_51 yobs_52 yobs_53 yobs_54 yobs_55 yobs_56 yobs_57 yobs_58 yobs_59 yobs_60 yobs_61 yobs_62 yobs_63 yobs_64 yobs_65 yobs_66 yobs_67 yobs_68 yobs_69 yobs_70 yobs_71 yobs_72 yobs_73 yobs_74 yobs_75 yobs_76 yobs_77 yobs_78 yobs_79 yobs_80 yobs_81 yobs_82 yobs_83 yobs_84 yobs_85 yobs_86 yobs_87 yobs_88 yobs_89 yobs_90 yobs_91 yobs_92 yobs_93 yobs_94 yobs_95 yobs_96 yobs_97 yobs_98 yobs_99 
	foreach x of local varlist {
		replace `x' = . if `x'>=survey_year & survey_year!=.
	}
	
	forvalues i = 1/25 {
		gen pre_yob_`i' = .
		replace pre_yob_`i' = yob-`i' if yob!=-99
	}
	
	rename survey_year year_prev
	forvalues i = 0/`=scalar(age)' {
		rename yobs_`i' year
		cap merge m:1 ccode year using "$data_dir/raw/`data'.dta", keepusing(`var') nogen
		rename `var' `var'_`i'
		rename year yobs_`i' 
		drop if iso2==""
		di "yob`i' merged"
	}
	rename year_prev survey_year
	
	cap drop yobs_*
	
	rename survey_year year_prev
	forvalues i = 1/25 {
		rename pre_yob_`i' year
		cap merge m:1 ccode year using "$data_dir/raw/`data'.dta", keepusing(`var') nogen
		rename `var' py_`var'_`i'
		rename year pre_yob_`i'
		drop if iso2==""
		di "pre yob`i' merged"
	}
	rename year_prev survey_year
	
	keep ccode survey_year yob age `var'_*  py_`var'_*
	
	compress
	sort ccode survey_year yob

end

****************************************************************************
**CREATE MERGE PROGRAM WITH JUST LIFETIME EXPERIENCES (nmerge)
****************************************************************************

capture program drop nmerge
program define nmerge

	args data var
	clear
	use "$data_dir/clean/harmonized_dataset.dta"
	
	replace age=18 if yob==2002 & age==17 & ccode=="EGY" & survey_year==2020
	
	gen test_dummy = 1 
	collapse test_dummy if yob!=-99, by(iso2 ccode survey_year yob age)
	drop test_dummy
	
	scalar age = 99
	cap drop yobs*
	forvalues i = 0/`=scalar(age)' {
		gen yobs_`i' = .
	}
	
	local j = 0
		while `j' <= `=scalar(age)' {
			replace yobs_`j'=yob+`j' if yob!=-99
			local j = `j' + 1
	}
	
	local varlist yobs_0 yobs_1 yobs_2 yobs_3 yobs_4 yobs_5 yobs_6 yobs_7 yobs_8 yobs_9 yobs_10 yobs_11 yobs_12 yobs_13 yobs_14 yobs_15 yobs_16 yobs_17 yobs_18 yobs_19 yobs_20 yobs_21 yobs_22 yobs_23 yobs_24 yobs_25 yobs_26 yobs_27 yobs_28 yobs_29 yobs_30 yobs_31 yobs_32 yobs_33 yobs_34 yobs_35 yobs_36 yobs_37 yobs_38 yobs_39 yobs_40 yobs_41 yobs_42 yobs_43 yobs_44 yobs_45 yobs_46 yobs_47 yobs_48 yobs_49 yobs_50 yobs_51 yobs_52 yobs_53 yobs_54 yobs_55 yobs_56 yobs_57 yobs_58 yobs_59 yobs_60 yobs_61 yobs_62 yobs_63 yobs_64 yobs_65 yobs_66 yobs_67 yobs_68 yobs_69 yobs_70 yobs_71 yobs_72 yobs_73 yobs_74 yobs_75 yobs_76 yobs_77 yobs_78 yobs_79 yobs_80 yobs_81 yobs_82 yobs_83 yobs_84 yobs_85 yobs_86 yobs_87 yobs_88 yobs_89 yobs_90 yobs_91 yobs_92 yobs_93 yobs_94 yobs_95 yobs_96 yobs_97 yobs_98 yobs_99
	foreach x of local varlist {
		replace `x' = . if `x'>=survey_year & survey_year!=.
	}
	
	rename survey_year year_prev
	forvalues i = 0/`=scalar(age)' {
		rename yobs_`i' year
		cap merge m:1 ccode year using "$data_dir/raw/`data'.dta", keepusing(`var') nogen
		rename `var' `var'_`i'
		rename year yobs_`i' 
		drop if iso2==""
		di "yob`i' merged"
	}
	rename year_prev survey_year
	
	cap drop yobs_*
	
	keep ccode survey_year yob age `var'_* 
	
	compress
	sort ccode survey_year yob

end


****************************************************************************
**GROWTH DATASETS
****************************************************************************

//MADISSON PROJECT DATABASE
local varlist growth_rate
foreach x of local varlist{
	dmerge mpd2023_clean `x'
	save "$data_dir/raw/experience_datasets/`x'_mpd_exp.dta", replace
}

local varlist growth_shock disaster acceleration
foreach x of local varlist{
	nmerge mpd2023_clean `x'
	save "$data_dir/raw/experience_datasets/`x'_mpd_exp.dta", replace
}

//PENN WORLD TABLES AND BARRO-URSUA
local varlist growth_rate
foreach x of local varlist{
	dmerge pwt100_barro_clean `x'
	save "$data_dir/raw/experience_datasets/`x'_pwt_barro_exp.dta", replace
}

****************************************************************************
**OTHER ECONOMIC DATASETS
****************************************************************************

//FINANCIAL CRISES DATA
local varlist bank_crisis currency_crisis twin_crisis bank_crisis_w currency_crisis_w twin_crisis_w
foreach x of local varlist {
	nmerge financial_crises_clean `x'
	save "$data_dir/raw/experience_datasets/`x'_exp.dta", replace
}

//GLOBAL FINANCIAL DATA
local varlist inflation winsz_inflation 
foreach x of local varlist {
	nmerge gfd_inflation_final `x'
	save "$data_dir/raw/experience_datasets/`x'_exp.dta", replace
}

****************************************************************************
**POLITICAL DATASETS
****************************************************************************

//BANKS CNTS DATA ARCHIVE
local varlist prop_unrest no_exec_changes
foreach x of local varlist {
	nmerge banks_cnts `x'
	save "$data_dir/raw/experience_datasets/`x'_exp.dta", replace
}

//V-DEM
local varlist democracy v2x_polyarchy v2xel_frefair v2x_jucon v2xlg_legcon v2mecenefm v2x_freexp
foreach x of local varlist {
	nmerge vdem_democracy `x'
	save "$data_dir/raw/experience_datasets/`x'_exp.dta", replace
}

//EM-DAT
local varlist prop_affect_epidemic
foreach x of local varlist {
	nmerge natural_shocks `x'
	save "$data_dir/raw/experience_datasets/`x'_exp.dta", replace
}

